package com.hp.dao;

import com.hp.entity.Room;
import com.hp.entity.dto.RoomDto;
import com.hp.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class RoomDao {
    public RoomDto getRoom(String sign, String departmentId, String userId) throws RuntimeException{
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="SELECT r.id as id,r.department_id as departmentId,r.name as name," +
                "r.created_time as createdTime,r.updated_time as updatedTime,s.id as scheduleId " +
                "FROM room r " +
                "LEFT JOIN SCHEDULE s ON r.id=s.room_id " +
                "LEFT JOIN sys_users u ON s.user_id=u.id AND s.department_id=u.department_id " +
                "WHERE s.sign=? AND s.department_id=? AND u.id=?";
        try {
            return queryRunner.query(sql,new BeanHandler<RoomDto>(RoomDto.class),sign,departmentId,userId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Room getRoom(Integer roomId) {
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="SELECT r.id as id,r.department_id as departmentId,r.name as name," +
                "r.created_time as createdTime,r.updated_time as updatedTime " +
                "FROM room r " +
                "WHERE r.id=?";
        try {
            return queryRunner.query(sql,new BeanHandler<Room>(Room.class),roomId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
